package com.ljh.service.Impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.mapper.SingerMapper;
import com.ljh.mapper.UserMapper;
import com.ljh.pojo.Singer;
import com.ljh.pojo.User;
import com.ljh.pojo.UserBefore;
import com.ljh.pojo.admin;
import com.ljh.service.SingerService;
import com.ljh.service.UserService;
import com.ljh.vo.SexVo;
import com.ljh.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserVo findAUserById(Integer id) {
        return userMapper.findAUserById(id);
    }

    /**
     * 微信登录
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code){
//        JSONObject entries = JSONUtil.parseObj(code);
//         code = (String) entries.get("code");
//        System.out.println();
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        HashMap map = new HashMap();
        map.put("appid","wx33b250fca50f0c11");
        map.put("secret","9fc0be7e1e9d5f0cb4671eb354f9f24e");
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String response = HttpUtil.post(url, map);
        System.out.println(response);
        JSONObject json =  JSONUtil.parseObj(response);
        String open_id = json.getStr("openid");
        if (open_id == null || open_id.length() == 0){
//            throw new ServiceException("临时登录凭证错误");
            System.out.println("临时登录凭证错误");
        }
        return open_id;
    }

    @Override
    public User userLogin(UserBefore userBefore) {
        User userByPassword = userMapper.findUserByPassword(userBefore.getUserPassword());
        if(userByPassword==null){
            userMapper.insertUser(userBefore);
            return userMapper.findUserByPassword(userBefore.getUserPassword());
        }else {
            return userByPassword;
        }
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public Integer UpdatePersonInfo(User user) {
        return userMapper.UpdatePersonInfo(user);
    }

    @Override
    public User findById(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public Singer findBySongId(Integer singId) {
        return userMapper.findBySongId(singId);
    }

    /**
     * 通过id来查询user
     * @param userId
     * @return
     */
    @Override
    public User findUserById(Integer userId) {
        return userMapper.findUserById(userId);
    }

    @Override
    public Integer UpdatePersonInfo2(User user) {
        return userMapper.UpdatePersonInfo2(user);
    }

    @Override
    public void UpdatePersonPhoto(String userPhoto, Integer userId) {
        userMapper.UpdatePersonPhoto(userPhoto,userId);
    }

    @Override
    public void deleteUserById(Integer userId) {
        userMapper.deleteUserById(userId);
    }

    @Override
    public List<SexVo> findUserSex() {
        return userMapper.findUserSex();
    }

    @Override
    public List<admin> loginMI(String password,String username) {
        return userMapper.loginMI(password,username);
    }


}
